import BaseIndex from '../baseIndex';

export default class EntityList extends BaseIndex {
    constructor(props) {
        super(props);

        this.init();

        this.initExpand();
    }

    searchQuery(id, actionType, data) {
        const { url, QueryInfo, dataAction } = data;
        const payload = { action: this.getAction(id, actionType, false) };
        payload.action.dataName = data.entityName;
        payload.action.dataGridViewSearchQuery = data.dataGridViewSearchQuery;

        payload.url = url;
        payload.QueryInfo = QueryInfo;

        let serviceName = this.serviceName;
        let actionName = 'searchQuery';
        if (dataAction && dataAction.serviceInterface) {
            serviceName = dataAction.serviceInterface.serviceName;
            actionName = dataAction.serviceInterface.actionName;
        }

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    setsearchQuery(id, actionType, data) {
        const dataName = data.action ? data.action.dataName : '';
        actionType = data.action.dataGridViewSearchQuery;
        data = this.setSearchQueryResponse(data, dataName);
        this.dispatchAction(id, actionType, data);
        return false;
    }

    deleteEntityData(id, actionType, data) {
        const { dataAction } = data;
        let entity = data.entity;

        if (dataAction && dataAction.entityName) {
            const { entityName, primaryKey, deleteMethodName } = dataAction;
            entity = { name: entityName, primaryKey, expandMethods: { deleteMethodName } };
        }

        const { expandMethods } = entity;
        if (expandMethods && expandMethods.isBatchDelete) {
            this.batchDeleteEntityData(id, actionType, data);
            return;
        }
        const primaryKeyValue = data.selectRowKeys[0];
        const { RowVersion } = data.selectDataList[0];

        let serviceName = this.serviceName;
        let actionName = 'delete';

        let url = '';
        if (dataAction) {
            const { serviceInterface } = dataAction;
            serviceName = serviceInterface.serviceName;
            actionName = serviceInterface.actionName;
            if (serviceInterface.url === 'EntityService') url = entity.name;
        }

        const method = entity.expandMethods && entity.expandMethods.deleteMethodName ? "/" + entity.expandMethods.deleteMethodName : '';
        let pathQuery = `${method}(${primaryKeyValue})?RowVersion=${RowVersion}`;
        if (entity.name === 'EntityFormData') {
            const { EntityFormId } = data.selectDataList[0];
            pathQuery += "&entityFormId=" + EntityFormId;
        }

        if (dataAction && dataAction.parameterNames) {
            dataAction.parameterNames.split(',').forEach(p => {
                const v = data.selectDataList[0][p];
                if (v) pathQuery += `&${p}=${encodeURIComponent(v)}`;
            });
        }

        const payload = { action: this.getAction(id, actionType) };
        if (url) payload.url = url + pathQuery;
        else payload.pathQuery = pathQuery;

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    updateEntityData(id, actionType, data) {
        const { dataAction, oldEntityData } = data;
        let entity = data.entity;

        if (dataAction && dataAction.entityName) {
            const { entityName, primaryKey } = dataAction;
            entity = { name: entityName, primaryKey };
        }

        const primaryKeyValue = data.selectRowKeys ? data.selectRowKeys[0] : oldEntityData[entity.primaryKey];
        const { RowVersion } = data.selectDataList ? data.selectDataList[0] : oldEntityData;

        const entityData = data.entityData || {};
        entityData[entity.primaryKey] = primaryKeyValue;
        entityData.RowVersion = RowVersion;

        let serviceName = this.serviceName;
        let actionName = 'updateEntityData';

        let url = '';
        if (dataAction) {
            const serviceInterface = dataAction.updateServiceInterface || dataAction.serviceInterface;
            serviceName = serviceInterface.serviceName;
            actionName = serviceInterface.actionName;

            if (serviceInterface.url === 'EntityService') url = entity.name;
        }

        const methodName = dataAction.updateMethodName ? '/' + dataAction.updateMethodName : '';

        const pathQuery = `${methodName}(${primaryKeyValue})`;

        const payload = { action: this.getAction(id, actionType) };
        payload[entity.name] = entityData;
        if (url) payload.url = url + pathQuery;
        else payload.pathQuery = pathQuery;

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    batchDeleteEntityData(id, actionType, data) {
        const { entity } = data;

        const entityDataList = entity.isIds ? data.selectDataList.map(m => ({ [entity.primaryKey]: m[entity.primaryKey], RowVersion: m.RowVersion })) : data.selectDataList;
        const pathQuery = '/' + entity.expandMethods.delete;

        this.dvaActions.dispatch(this.serviceName, 'delete', { pathQuery, [entity.name]: entityDataList, action: this.getAction(id, actionType) });
    }

    excelExport(id, actionType, data) {
        const { dataAction } = data;
        data.action = this.getAction(id, actionType, false);
        data.action.dataGridViewExcelExport = data.dataGridViewExcelExport;

        delete data.dataGridViewExcelExport;
        delete data.dataAction;

        let serviceName = this.serviceName;
        let actionName = 'excelExport';
        if (dataAction && dataAction.serviceInterface) {
            serviceName = dataAction.serviceInterface.serviceName;
            actionName = dataAction.serviceInterface.actionName;
        }

        this.dvaActions.dispatch(serviceName, actionName, data);
    }

    setexcelExport(id, actionType, data) {
        actionType = data.action.dataGridViewExcelExport;
        this.dispatchAction(id, actionType, data);
        return false;
    }

    saveEntityData(id, actionType, data) {
        const { dataAction } = data;
        if (dataAction && dataAction.entityName) {
            const { entityName, primaryKey, insertMethodName, updateMethodName } = dataAction;
            data.entity = { name: entityName, primaryKey, expandMethods: { insert: insertMethodName, update: updateMethodName } };
        }
        const { name, primaryKey, expandMethods } = data.entity;
        const primaryKeyValue = data.oldEntityData && data.oldEntityData[primaryKey] ? data.oldEntityData[primaryKey] : null;

        if (data.oldEntityData && data.oldEntityData.RowVersion) data.entityData.RowVersion = data.oldEntityData.RowVersion;

        let serviceName = this.serviceName;
        let actionName = primaryKeyValue ? "update" : "insert";

        let url = '';
        if (dataAction) {
            const { insertServiceInterface, updateServiceInterface } = dataAction;
            const serviceInterface = primaryKeyValue ? updateServiceInterface : insertServiceInterface;
            serviceName = serviceInterface.serviceName;
            actionName = serviceInterface.actionName;
            if (serviceInterface.url === 'EntityService') url = name;
        }

        let method = expandMethods && expandMethods.insert ? "/" + expandMethods.insert : "";
        if (primaryKeyValue) method = expandMethods && expandMethods.update ? "/" + expandMethods.update : "";

        let pathQuery = method;
        if (primaryKeyValue) {
            data.entityData[primaryKey] = primaryKeyValue;
            pathQuery = `${method}(${primaryKeyValue})`;
        }

        const payload = { action: this.getAction(id, actionType) };
        payload[name] = data.entityData;
        if (url) payload.url = url + pathQuery;
        else payload.pathQuery = pathQuery;

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    batchUpdateEntityData(id, actionType, data) {
        const { selectData, dataAction, selectRowKeys } = data;
        const entityData = { Ids: selectRowKeys, ...selectData };

        const { entityName } = dataAction;

        const payload = { action: this.getAction(id, actionType) };
        payload[entityName] = entityData;

        const { serviceName, actionName } = dataAction.serviceInterface;

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    getEntityData(id, actionType, data) {
        const { dataAction, entityData } = data;
        const { name, primaryKey } = data.entity;
        let expandMethods = data.entity.expandMethods;
        if (dataAction && dataAction.getEntityData) expandMethods = { getEntityData: dataAction.getEntityData };

        const method = expandMethods && expandMethods.getEntityData ? "/" + expandMethods.getEntityData : "";
        const primaryKeyValue = entityData[primaryKey];
        let pathQuery = `${method}(${primaryKeyValue})`;

        let serviceName = this.serviceName;
        let actionName = 'getEntityData';

        let url = '';
        if (dataAction) {
            const { serviceInterface } = dataAction;
            serviceName = serviceInterface.serviceName;
            actionName = serviceInterface.actionName;
            if (serviceInterface.url === 'EntityService') url = name;
        }

        if (entityData.queryString) pathQuery += "?" + entityData.queryString;

        const payload = { action: this.getAction(id, actionType) };
        if (url) payload.url = url + pathQuery;
        else payload.pathQuery = pathQuery;

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }
}
